<?php

namespace app\controllers;

use Yii;
use app\models\ChatAssigned;
use app\models\User;
use app\models\ChatSource;
use app\models\ChatAssignedSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ChatAssignedController implements the CRUD actions for ChatAssigned model.
 */
class ChatAssignedController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all ChatAssigned models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new ChatAssignedSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single ChatAssigned model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    public function actionCreate1() {
        $model = new ChatAssigned();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {

            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Creates a new ChatAssigned model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new ChatAssigned();
        if (Yii::$app->request->isPost) {
//        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $model->user_id = $_POST['user_id'];
            $model->source = $_POST['source'];
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        } else {
            //客服select2数据
            $userModel = new User();
            $userInfo = $userModel->getAllUserArr(['id', 'username']);
            $userData = array();
            foreach ($userInfo as $k) {
                $userData[$k['id']] = $k['username'];
            }
            //渠道select2数据
            $sourceModel = new ChatSource();
            $sourceInfo = $sourceModel->getAllSourceArr(['source_value', 'description']);
            $sourceData = array();
            foreach ($sourceInfo as $k) {
                $sourceData[$k['source_value']] = $k['description'];
            }
            return $this->render('create', [
                        'model' => $model,
                        'userData' => $userData,
                        'sourceData' => $sourceData,
            ]);
        }
    }

    /**
     * Updates an existing ChatAssigned model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);
        if (Yii::$app->request->isPost) {
            $model->user_id = $_POST['user_id'];
            $model->source = $_POST['source'];
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        } else {
            //客服select2数据
            $userModel = new User();
            $userInfo = $userModel->getAllUserArr(['id', 'username']);
            $userData = array();
            foreach ($userInfo as $k) {
                $userData[$k['id']] = $k['username'];
            }
            //渠道select2数据
            $sourceModel = new ChatSource();
            $sourceInfo = $sourceModel->getAllSourceArr(['source_value', 'description']);
            $sourceData = array();
            foreach ($sourceInfo as $k) {
                $sourceData[$k['source_value']] = $k['description'];
            }
            return $this->render('update', [
                        'model' => $model,
                        'userData' => $userData,
                        'sourceData' => $sourceData,
            ]);
        }
    }

    /**
     * Deletes an existing ChatAssigned model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the ChatAssigned model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ChatAssigned the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = ChatAssigned::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}
